library(broom)
library(lme4)
library(emmeans)
library(plyr)
library(tidyverse)
library(magrittr)

# figure 1

t1 <- "C:/Dropbox/ethankimtom/pc methods paper/replication code and data/t1.RDS" %>% 
  readRDS

l_m <- t1 %>% 
  dlply(
    .(cond2),
    failwith(
      NULL,
      function(j)
        glmer(
          ans ~ appeared*durat + network + (1 | host),
          data = j,
          family = binomial()
        )
      )
    )

eg2 <- expand.grid(
  appeared = 0:1,
  network = 0:1,
  durat = c("post", "nopost")
  ) %>% 
  filter(
    (appeared == 1 &
      network == 0
    ) %>% 
      not
  )


pd1 <- l_m %>% 
  map_df(
    function(i)
      eg2 %>% 
      mutate(
        prob = predict(
          i, 
          newdata = eg2,
          re.form = NA,
          type = "response"
          )
      )
  ) %>% 
  mutate(
    cond = c("FNC",
             "MSNBC") %>% 
      rep(each = 6) %>% 
      mapvalues(
        c("FNC", "MSNBC"),
        c("Subjects assigned Fox News",
          "Subjects assigned MSNBC")
      ),
    durat = durat %>% 
      mapvalues(
        c("post", "nopost"),
        c("Respondent assigned debate and commentary",
          "Respondent assigned debate only")
      ) %>% 
      factor(
        c("Respondent assigned debate and commentary",
          "Respondent assigned debate only")
      ),
    network = network  %>%  
      mapvalues(
        1:0,
        c("Compere affiliated with network",
          "Compere not affiliated with network")
      ) %>% 
      factor(
        rev(
          c("Compere affiliated with network",
            "Compere not affiliated with network")
        )
      ),
    appeared = appeared  %>%  
      mapvalues(
        1:0,
        c("Compere appeared",
          "Compere did not appear")
      ) %>% 
      factor(
        rev(
          c("Compere appeared",
            "Compere did not appear")
        )
      )
  ) %>% 
  tbl_df

pd1$lab <- pd1$prob %>% 
  round(2) %>% 
  as.character %>% 
  str_sub(2) %>% 
  str_pad(width = 3, "right", "0")

edf <- l_m %>% 
  map_df(
    function(i){
      i %>% 
        emmeans(
          ~ appeared * durat * network,
          at = list(
            durat = c(0, 1)
            )
        ) %>% 
        pairs %>% 
        regrid %>% 
        tidy
    }
  )

pd1l <- pd1 %>% 
  filter(
    network %>% 
      str_detect("not affiliated") %>% 
      not
  ) %>% 
  mutate(
    appeared = appeared %>% 
      as.numeric %>% 
      mapvalues(1:2,
                c("start", "end"))
  ) %>% 
  select(-lab) %>% 
  spread(appeared, prob) %>% 
  mutate(mid = start %>% 
           add(end) %>%
           divide_by(2),
         plab =  c("p = .01", "p = .007", "", "")
  )

# figure 1's quantities are available in objecgs pd1, pd1l, and edf

# table 2

t2 <- "C:/Dropbox/ethankimtom/pc methods paper/replication code and data/t2.RDS" %>% 
  readRDS

t3 <- t2 %>% 
  select(WorkerID, cond2, partyid, wave) %>%
  unite(cw, cond2, wave, remove = F) %>%
  mutate(cw = cw %>%
           fct_inorder
  ) %>%
  group_by(cw, partyid) %>% 
  tally %>%
  mutate(
    w = cw %>% 
      str_sub(-2)
  ) %>% 
  arrange(w, cw) %>% 
  group_by(w) %>% 
  dplyr::mutate(
    n = n %>% 
      divide_by(
        n %>% 
          sum
      ) %>% 
      multiply_by(100) %>% 
      round
  ) %>%
  ungroup %>% 
  select(-w) %>% 
  spread(cw, n) %>% 
  rename(cat = partyid) %>% 
  rbind(
    t2 %>% 
      select(WorkerID, cond2, ideology, wave) %>% 
      unite(cw, cond2, wave, remove = F) %>%
      mutate(cw = cw %>%
               fct_inorder
      ) %>%
      group_by(cw, ideology) %>% 
      tally %>%
      mutate(
        w = cw %>% 
          str_sub(-2)
      ) %>% 
      arrange(w, cw) %>% 
      group_by(w) %>% 
      dplyr::mutate(
        n = n %>% 
          divide_by(
            n %>% 
              sum
          ) %>% 
          multiply_by(100) %>% 
          round
      ) %>%
      ungroup %>% 
      select(-w) %>% 
      spread(cw, n) %>% 
      rename(cat = ideology),
    t2 %>% 
      select(WorkerID, cond2, race, wave) %>% 
      unite(cw, cond2, wave, remove = F) %>%
      mutate(cw = cw %>%
               fct_inorder
      ) %>%
      group_by(cw, race) %>% 
      tally %>%
      mutate(
        w = cw %>% 
          str_sub(-2)
      ) %>% 
      arrange(w, cw) %>% 
      group_by(w) %>% 
      dplyr::mutate(
        n = n %>% 
          divide_by(
            n %>% 
              sum
          ) %>% 
          multiply_by(100) %>% 
          round
      ) %>%
      ungroup %>% 
      select(-w) %>% 
      spread(cw, n) %>% 
      rename(cat = race),
    t2 %>% 
      select(WorkerID, cond2, votechoicew1, wave) %>% 
      unite(cw, cond2, wave, remove = F) %>%
      mutate(cw = cw %>%
               fct_inorder
      ) %>%
      group_by(cw, votechoicew1) %>% 
      tally %>%
      mutate(
        w = cw %>% 
          str_sub(-2)
      ) %>% 
      arrange(w, cw) %>% 
      group_by(w) %>% 
      dplyr::mutate(
        n = n %>% 
          divide_by(
            n %>% 
              sum
          ) %>% 
          multiply_by(100) %>% 
          round
      ) %>%
      ungroup %>% 
      select(-w) %>% 
      spread(cw, n) %>% 
      rename(cat = votechoicew1),
    t2 %>% 
      select(WorkerID, cond2, inc2, wave) %>% 
      unite(cw, cond2, wave, remove = F) %>%
      mutate(cw = cw %>%
               fct_inorder
      ) %>%
      group_by(cw, inc2) %>% 
      tally %>%
      mutate(
        w = cw %>% 
          str_sub(-2)
      ) %>% 
      arrange(w, cw) %>% 
      group_by(w) %>% 
      dplyr::mutate(
        n = n %>% 
          divide_by(
            n %>% 
              sum
          ) %>% 
          multiply_by(100) %>% 
          round
      ) %>%
      ungroup %>% 
      select(-w) %>% 
      spread(cw, n) %>% 
      rename(cat = inc2),
    t2 %>% 
      select(WorkerID, cond2, age2, wave) %>% 
      unite(cw, cond2, wave, remove = F) %>%
      mutate(cw = cw %>%
               fct_inorder
      ) %>%
      group_by(cw, age2) %>% 
      tally %>%
      mutate(
        w = cw %>% 
          str_sub(-2)
      ) %>% 
      arrange(w, cw) %>% 
      group_by(w) %>% 
      dplyr::mutate(
        n = n %>% 
          divide_by(
            n %>% 
              sum
          ) %>% 
          multiply_by(100) %>% 
          round
      ) %>%
      ungroup %>% 
      select(-w) %>% 
      spread(cw, n) %>% 
      rename(cat = age2),
    t2 %>% 
      select(WorkerID, cond2, channelpreferencew1, wave) %>% 
      unite(cw, cond2, wave, remove = F) %>%
      mutate(cw = cw %>%
               fct_inorder
      ) %>%
      group_by(cw, channelpreferencew1) %>% 
      tally %>%
      mutate(
        w = cw %>% 
          str_sub(-2)
      ) %>% 
      arrange(w, cw) %>% 
      group_by(w) %>% 
      dplyr::mutate(
        n = n %>% 
          divide_by(
            n %>% 
              sum
          ) %>% 
          multiply_by(100) %>% 
          round
      ) %>%
      ungroup %>% 
      select(-w) %>% 
      spread(cw, n) %>% 
      rename(cat = channelpreferencew1)
  ) %>% 
  na.omit

t3$cat %<>% 
  mapvalues(
    t3$cat %>% 
      unique,
    c("Democrat",
      "Independent",
      "Republican",
      
      "Conservative",
      "Liberal",
      "Moderate",
      
      "Black",
      "Hispanic",
      "Other",
      "White",
      
      "Clinton",
      "Trump",
      
      "<$30k",
      "$30-60k",
      "$60-90k",
      ">$90k",
      
      "18-34",
      "35-49",
      "50-64",
      "65+",
      
      "C-SPAN",
      "CNN",
      "Fox News",
      "MSNBC",
      "None")
  ) %>% 
  factor(
    c("Democrat",
      "Independent",
      "Republican",
      
      "Liberal",
      "Moderate",
      "Conservative",
      
      "Black",
      "Hispanic",
      "Other",
      "White",
      
      "Clinton",
      "Trump",
      
      "<$30k",
      "$30-60k",
      "$60-90k",
      ">$90k",
      
      "18-34",
      "35-49",
      "50-64",
      "65+",
      
      "Fox News",
      "CNN",
      "MSNBC",
      "C-SPAN",
      "None")
  )

t3 %<>% 
  arrange(cat)

# figure 3

t4 <- "C:/Dropbox/ethankimtom/pc methods paper/replication code and data/t3.RDS" %>% 
  readRDS

mod_tab <- data.frame(
  study = t4$study %>% 
    unique
) %>% 
  tbl_df %>% 
  mutate(
    study = study %>% 
      as.character
  )

mod_tab$preds <- mod_tab$study %>% 
  map(function(i)
    t4 %>% 
      filter(study == i) %>% 
      select(-caseid, -dv_wave2_partic) %>% 
      gather(var, val, -study, na.rm = T) %>% 
      use_series(var) %>% 
      unique %>% 
      unlist %>% 
      str_c(collapse = " + ")
  )

mod_tab$form <- 1:7 %>% 
  map(function(i)
    str_c("dv_wave2_partic  ~ ",
          mod_tab$preds[[i]])) %>% 
  unlist

mod_tab$mods <- 1:7 %>% 
  map(
    possibly(
      function(i)
    glm(
      mod_tab$form[[i]],
      data = t4 %>%
        filter(study == mod_tab$study[[i]]),
        family = binomial()
      ),
    NULL)
    )

mod_tab$coefs <- mod_tab %>% 
  use_series(mods) %>% 
  map(function(i)
    i %>% 
      tidy %>% 
      filter(term != "(Intercept)")
    )

lt <- map2_df(
  mod_tab$coefs,
  mod_tab$study,
  function(i, j)
    i %>% 
    mutate(study = j)
  )

ldf <- lt %>% 
  select(term, estimate, study) %>% 
  spread(term, estimate) %>%
  select(-1) %>% 
  mutate(age = age %>% 
           is.na %>% 
           ifelse(
             age %>% 
               mean(na.rm = T),
             age)
  ) %>%
  psych::fa() %>% 
  loadings %>% 
  extract(1:7, ) %>% 
  as.data.frame %>% 
  set_colnames("loading") %>% 
  rownames_to_column %>% 
  arrange(desc(loading))

lt$term %<>% 
  factor(ldf$rowname)

lt$estimate2 <- lt$estimate %>% abs

lt %<>% 
  arrange(study, term)

lt$term %<>% 
  mapvalues(lt$term %>% 
              levels,
            c("Education",
              "Ideology",
              "Partisanship",
              "Age",
              "Income",
              "Male",
              "Non-white")) %>% 
  factor(
    c("Education",
      "Ideology",
      "Partisanship",
      "Age",
      "Income",
      "Male",
      "Non-white")
    )

lt$study %<>% 
  mapvalues(
    lt$study %>% 
      unique,
    lt$study %>% 
      unique %>% 
      extract(-7) %>% 
      str_to_upper %>% 
      c("MTurk debate study")
  ) %>% 
  factor(
    lt$study %>% 
      unique %>% 
      extract(-7) %>% 
      str_to_upper %>% 
      c("MTurk debate study")
  )

lt$year <- lt$study %>% 
  str_extract("\\d{4}")

lt$year[lt$study %>% 
          str_detect("Turk")] <- "2016"


# item lt contains all necessary information figure 3


# table 3

t5 <- "C:/Dropbox/ethankimtom/pc methods paper/replication code and data/t4.RDS" %>% 
  readRDS 


t5 %>% 
  select(-cond, -channel2) %>% 
  gather(
    wave, part, matches("w\\d")
  ) %>% 
  group_by(party2, chan, wave, part) %>% 
  tally %>% 
  filter(
    part == 1
  ) %>% 
  select(-part) %>% 
  spread(wave, n) %>%
  ungroup %>% 
  cbind(
    t5 %>% 
      select(-cond, -channel2) %>% 
      gather(
        wave, part, matches("w\\d")
      ) %>% 
      group_by(party2, chan, wave, part) %>% 
      tally %>% 
      mutate(prop = n %>% 
               divide_by(
                 n %>% 
                   sum
               ) %>% 
               multiply_by(100) %>% 
               round(1)) %>% 
      filter(wave != "w1" &
               part == 1) %>% 
      select(-part, -n) %>%
      ungroup %>% 
      mutate(
        wave = wave %>% 
          mapvalues(
            str_c("w", 2:3),
            str_c("pc", 2:3)
          )
      ) %>%
      spread(wave, prop) %>% 
      select(starts_with("pc"))
  ) %>% 
  mutate(df1 = pc2 - 100,
         df2 = pc3 - pc2)

# appendix figure 1

t6 <- "C:/Dropbox/ethankimtom/pc methods paper/replication code and data/t5.RDS" %>% 
  readRDS

sf <- expand.grid(
  age = t6$age %>% 
    mean,
  ed_attain = t6$ed_attain %>%
    mean,
  race = 1,
  gender = 1,
  income = t6$income %>% 
    mean,
  partyid = t6$partyid %>% 
    mean,
  ideology =  t6$ideology %>% 
    mean,
  cond = t6$cond %>% 
    levels
  )

expand.grid.df <- function(...) Reduce(function(...) merge(..., by=NULL), list(...))


cov_tab <- tibble(
  cov = names(t6)[3:9]
  ) %>% 
  mutate(ldf = names(t6)[3:9] %>%
           map(
             # i <- names(d_com)[3:9][[1]]
             function(i){
               
               if(i %>% 
                  is_in(
                    c("race",
                      "gender")
                    )
                  ) {
                 
                 sf2 <- sf %>%
                   select(-i)
                 
                 n2 <- data.frame(v1 = c(0, 1)) %>% 
                   set_colnames(i)
                 
                 expand.grid.df(sf2, n2) %>% 
                   arrange(cond)
                 
                 
               } else {
                 
                 sf2 <- sf %>%
                   select(-i)
                 
                 n2 <- data.frame(v1 = t6[[i]] %>%
                                    mean(na.rm = T) %>% 
                                    c(t6[[i]] %>%
                                        mean(na.rm = T) %>% 
                                        add(t6[[i]] %>%
                                              sd(na.rm = T)
                                        )
                                    )
                 ) %>% 
                   set_colnames(i)
                 
                 expand.grid.df(sf2, n2) %>% 
                   arrange(cond)
               }
             }
           )
         )

t7 <- t6 %>% 
  ddply(.(study),
        function(h)
          1000 %>% 
          rdply(
            function(){
              
              j <- glm(
                dv_wave2_partic ~ (age + ed_attain + race + gender + income + partyid + ideology)*cond,
                family = binomial(),
                data =  h %>%
                  sample_frac(1, T)
              )
              
              cov_tab$ldf %>% 
                map_df(function(k)
                  k %>% 
                    mutate(fit = predict(j, newdata = k, type = "response") %>% 
                             as.numeric)
                ) %>% 
                mutate(
                  covar = names(d_com)[3:9] %>%
                    rep(each = 6),
                  rt = letters[1:2] %>%
                    rep(times = 21)
                ) %>% 
                select(cond, fit:rt) %>% 
                spread(rt, fit) %>% 
                mutate(eff = b - a) %>% 
                select(-a, -b)
            },
            .progress = "time")
  ) %>% 
  tbl_df


t8 <- t7 %>% 
  dlply(.(study, cond, covar),
        function(i)
          i$eff %>% 
          quantile(c(.005, .025, .5, .975, .995))) %>% 
  bind_rows %>% 
  mutate(quant = c("low",
                   "ml",
                   "mid",
                   "mh",
                   "high")) %>% 
  gather(est, val, -quant) 

t9 <- t8$est %>% 
  str_split(fixed("."), n = 3) %>%
  unlist %>% 
  matrix(ncol = 3, byrow = T) %>% 
  as.data.frame %>% 
  set_colnames(
    c("study",
      "cond",
      "covar")
  ) %>% 
  cbind(t8 %>% 
          select(-est)) %>% 
  tbl_df %>% 
  spread(quant, val)

t9$covar %<>% 
  mapvalues(
    t9$covar %>%
      levels,
    c("Education",
      "Income",
      "Republican",
      "Conservative",
      "Age",
      "Male",
      "Non-White")) %>% 
  factor(
    c("Education",
      "Income",
      "Republican",
      "Conservative",
      "Age",
      "Male",
      "Non-White")
    )

t9$covar %<>%
  factor(t9 %>%
           group_by(covar) %>% 
           summarize(mu = mid %>% 
                       mean) %>% 
           arrange(mu) %>% 
           use_series(covar)
  )


t9$study %<>% 
  mapvalues(t9$study %>% 
              levels,
            c("Presidential debate",
              "State of the Union"))

t10 <- t9 %>%
  filter(study %>% 
           str_detect("debate")) %>%
  rbind.fill(
    t9 %>%
      filter(study %>% 
               str_detect("debate")) %>% 
      group_by(cond) %>%
      summarise_at(
        c("high", "low", "mh", "mid", "ml"),
        function(i)
          i %>% 
          mean
      ) %>% 
      mutate(
        covar = "Overall",
        study = "Presidential debate"
      ) %>% 
      filter(
        cond != "CSPAN"
      )
  )

t10$covar %<>% 
  factor(
    c("Overall", 
      t10$covar %>% 
        levels
    )
  )

# object t10 has all the quantities necessary to plot the appendix table 1

t11 <- "C:/Dropbox/ethankimtom/pc methods paper/replication code and data/t6.RDS" %>% 
  readRDS

lm(val ~ cond * targ, data = t11) %>% 
  summary

# table a2

t12 <- "C:/Dropbox/ethankimtom/pc methods paper/replication code and data/t7.RDS" %>% 
  readRDS

l_m <- t12 %>% 
  dlply(
    .(cond2),
      function(j)
        glmer(
          ans ~ appeared*durat + network + (1 | host),
          data = j,
          family = binomial()
          )
    )

l_m %>% 
  stargazer(
    initial.zero = F,
    digits = 2,
    type = "text",
    column.labels = c("Fox News Conditions", "MSNBC Conditions"),
    omit = "Constant",
    model.numbers = F,
    covariate.labels = c("Personality appeared",
                         "Respondent assigned post-debate commentary",
                         "Personality affiliated with network",
                         "Personality appeared * Respondent Assigned post-debate commentary"
    ),
    # type = "latex", 
    dep.var.caption = "", 
    dep.var.labels.include = F
  )

# table a.3

t13 <- "C:/Dropbox/ethankimtom/pc methods paper/replication code and data/t8.RDS" %>% 
  readRDS

mod_tab <- data.frame(
  study = t13$study %>% 
    unique
) %>% 
  tbl_df %>% 
  mutate(
    study = study %>% 
      as.character
  )

mod_tab$preds <- mod_tab$study %>% 
  # extract(1) %>% 
  map(function(i)
    # i <- mod_tab$study[[1]]
    
    t13 %>% 
      filter(study == i) %>% 
      select(-caseid, -dv_wave2_partic) %>% 
      gather(var, val, -study, na.rm = T) %>% 
      use_series(var) %>% 
      unique %>% 
      unlist %>% 
      str_c(collapse = " + ")
  )

mod_tab$form <- 1:7 %>% 
  map(function(i)
    str_c("dv_wave2_partic  ~ ",
          mod_tab$preds[[i]])) %>% 
  unlist

mod_tab$mods <- 1:7 %>% 
  map(
    function(i)
        glm(
          mod_tab$form[[i]],
          data = t13 %>%
            filter(
              study == mod_tab$study[[i]]
              ),
        family = binomial()
        )
    )

mod_tab$mods %>% 
  stargazer(
    no.space = T,
    dep.var.labels = NULL,
    colnames = F,
    column.labels = c(2008, 2012, 2016,
                      2008, 2012, 2016,
                      "MTurk"),
    initial.zero = F,
    type = "text",
    digits = 2, 
    covariate.labels = c("Education",
                         "Income",
                         "Partisanship",
                         "Ideology",
                         "Race: Minority",
                         "Age",
                         "Male")
    )

# tables a.4 and a.5

t15 <- "C:/Dropbox/ethankimtom/pc methods paper/replication code and data/t10.RDS" %>% 
  readRDS

glm(
  partic ~ c2 * party3 * wave,
  data = t15 %>% 
    filter(
      study == "debate"
    ),
  family = binomial()
  ) %>% 
  stargazer(
    dep.var.caption = "",
    column.labels = NULL,
    dep.var.labels.include = F,
    type = "text",
    no.space = T,
    dep.var.labels = NULL,
    colnames = F,
    initial.zero = F, 
    omit =  "Constant",
    digits = 2,
    covariate.labels = c(
      "Cond: Fox News",
      "Cond: MSNBC",
      "Independent",
      "Republican",
      "Wave 3",
      "Fox News * Independent",
      "MSNBC * Independent",
      "Fox News * Republican",
      "MSNBC * Republican",
      "Fox News * Wave 3",
      "MSNBC * Wave 3",
      "Wave 3 * Independent",
      "Wave 3 * Republican",
      "Fox News * Independent * Wave 3",
      "MSNBC * Independent * Wave 3",
      "Fox News * Republican * Wave 3",
      "MSNBC * Republican * Wave 3")
  )


t14 <-   "C:/Dropbox/ethankimtom/pc methods paper/replication code and data/t9.RDS" %>% 
  readRDS




glm(
  dv_wave2_partic ~ (age + ed_attain + race + gender + income + partyid + ideology)*cond,
  family = binomial(),
  data = t14
  ) %>% 
  stargazer(
    type = "text",
    dep.var.caption = "",
    column.labels = NULL,
    dep.var.labels.include = F,
    no.space = T,
    dep.var.labels = NULL,
    colnames = F,
    initial.zero = F, 
    omit =  "Constant",
    digits = 2,
    covariate.labels = c(
      "Age",
      "Education",
      "Non-white",
      "Male",
      "Income",
      "Republican",
      "Conservative",
      "Condition (Fox News)",
      "Condition (MSNBC)",
      "Age * Condition (Fox News)",
      "Age * Condition (MSNBC)",
      "Education * Condition (Fox News)",
      "Education * Condition (MSNBC)",
      "Non-white * Condition (Fox News)",
      "Non-white * Condition (MSNBC)",
      "Male * Condition (Fox News)",
      "Male * Condition (MSNBC)",
      "Income * Condition (Fox News)",
      "Income * Condition (MSNBC)",
      "Republican * Condition (Fox News)",
      "Republican * Condition (MSNBC)",
      "Conservative * Condition (Fox News)",
      "Conservative * Condition (MSNBC)")
  )

t16 <-  "C:/Dropbox/ethankimtom/pc methods paper/replication code and data/t11.RDS" %>% 
  readRDS

t17 <- t16 %>% 
  select(WorkerID, cond2, partyid, wave) %>%
  unite(cw, cond2, wave, remove = F) %>%
  mutate(cw = cw %>%
           fct_inorder
  ) %>%
  group_by(cw, partyid) %>% 
  tally %>%
  mutate(
    w = cw %>% 
      str_sub(-2)
  ) %>% 
  arrange(w, cw) %>% 
  group_by(w) %>% 
  dplyr::mutate(
    n = n %>% 
      divide_by(
        n %>% 
          sum
      ) %>% 
      multiply_by(100) %>% 
      round
  ) %>%
  ungroup %>% 
  select(-w) %>% 
  spread(cw, n) %>% 
  rename(cat = partyid) %>% 
  rbind(
    t16 %>% 
      select(WorkerID, cond2, ideology, wave) %>% 
      unite(cw, cond2, wave, remove = F) %>%
      mutate(cw = cw %>%
               fct_inorder
      ) %>%
      group_by(cw, ideology) %>% 
      tally %>%
      mutate(
        w = cw %>% 
          str_sub(-2)
      ) %>% 
      arrange(w, cw) %>% 
      group_by(w) %>% 
      dplyr::mutate(
        n = n %>% 
          divide_by(
            n %>% 
              sum
          ) %>% 
          multiply_by(100) %>% 
          round
      ) %>%
      ungroup %>% 
      select(-w) %>% 
      spread(cw, n) %>% 
      rename(cat = ideology),
    t16 %>% 
      select(WorkerID, cond2, race, wave) %>% 
      unite(cw, cond2, wave, remove = F) %>%
      mutate(cw = cw %>%
               fct_inorder
      ) %>%
      group_by(cw, race) %>% 
      tally %>%
      mutate(
        w = cw %>% 
          str_sub(-2)
      ) %>% 
      arrange(w, cw) %>% 
      group_by(w) %>% 
      dplyr::mutate(
        n = n %>% 
          divide_by(
            n %>% 
              sum
          ) %>% 
          multiply_by(100) %>% 
          round
      ) %>%
      ungroup %>% 
      select(-w) %>% 
      spread(cw, n) %>% 
      rename(cat = race),
    t16 %>% 
      select(WorkerID, cond2, votechoicew1, wave) %>% 
      unite(cw, cond2, wave, remove = F) %>%
      mutate(cw = cw %>%
               fct_inorder
      ) %>%
      group_by(cw, votechoicew1) %>% 
      tally %>%
      mutate(
        w = cw %>% 
          str_sub(-2)
      ) %>% 
      arrange(w, cw) %>% 
      group_by(w) %>% 
      dplyr::mutate(
        n = n %>% 
          divide_by(
            n %>% 
              sum
          ) %>% 
          multiply_by(100) %>% 
          round
      ) %>%
      ungroup %>% 
      select(-w) %>% 
      spread(cw, n) %>% 
      rename(cat = votechoicew1),
    t16 %>% 
      select(WorkerID, cond2, inc2, wave) %>% 
      unite(cw, cond2, wave, remove = F) %>%
      mutate(cw = cw %>%
               fct_inorder
      ) %>%
      group_by(cw, inc2) %>% 
      tally %>%
      mutate(
        w = cw %>% 
          str_sub(-2)
      ) %>% 
      arrange(w, cw) %>% 
      group_by(w) %>% 
      dplyr::mutate(
        n = n %>% 
          divide_by(
            n %>% 
              sum
          ) %>% 
          multiply_by(100) %>% 
          round
      ) %>%
      ungroup %>% 
      select(-w) %>% 
      spread(cw, n) %>% 
      rename(cat = inc2),
    t16 %>% 
      select(WorkerID, cond2, age2, wave) %>% 
      unite(cw, cond2, wave, remove = F) %>%
      mutate(cw = cw %>%
               fct_inorder
      ) %>%
      group_by(cw, age2) %>% 
      tally %>%
      mutate(
        w = cw %>% 
          str_sub(-2)
      ) %>% 
      arrange(w, cw) %>% 
      group_by(w) %>% 
      dplyr::mutate(
        n = n %>% 
          divide_by(
            n %>% 
              sum
          ) %>% 
          multiply_by(100) %>% 
          round
      ) %>%
      ungroup %>% 
      select(-w) %>% 
      spread(cw, n) %>% 
      rename(cat = age2),
    t16 %>% 
      select(WorkerID, cond2, channelpreferencew1, wave) %>% 
      unite(cw, cond2, wave, remove = F) %>%
      mutate(cw = cw %>%
               fct_inorder
      ) %>%
      group_by(cw, channelpreferencew1) %>% 
      tally %>%
      mutate(
        w = cw %>% 
          str_sub(-2)
      ) %>% 
      arrange(w, cw) %>% 
      group_by(w) %>% 
      dplyr::mutate(
        n = n %>% 
          divide_by(
            n %>% 
              sum
          ) %>% 
          multiply_by(100) %>% 
          round
      ) %>%
      ungroup %>% 
      select(-w) %>% 
      spread(cw, n) %>% 
      rename(cat = channelpreferencew1)
  ) %>% 
  na.omit

t17$cat %<>% 
  mapvalues(
    t17$cat %>% 
      unique,
    c("Democrat",
      "Independent",
      "Republican",
      
      "Conservative",
      "Liberal",
      "Moderate",
      
      "Black",
      "Hispanic",
      "Other",
      "White",
      
      "Clinton",
      "Trump",
      
      "<$30k",
      "$30-60k",
      "$60-90k",
      ">$90k",
      
      "18-34",
      "35-49",
      "50-64",
      "65+",
      
      "C-SPAN",
      "CNN",
      "Fox News",
      "MSNBC",
      "None")
  ) %>% 
  factor(
    c("Democrat",
      "Independent",
      "Republican",
      
      "Liberal",
      "Moderate",
      "Conservative",
      
      "Black",
      "Hispanic",
      "Other",
      "White",
      
      "Clinton",
      "Trump",
      
      "<$30k",
      "$30-60k",
      "$60-90k",
      ">$90k",
      
      "18-34",
      "35-49",
      "50-64",
      "65+",
      
      "Fox News",
      "CNN",
      "MSNBC",
      "C-SPAN",
      "None")
  )

t17 %<>% 
  arrange(cat)

# table a.9 is comprised of the objects t19 and t21 

t18 <- "C:/Dropbox/ethankimtom/pc methods paper/replication code and data/t12.RDS" %>% 
  readRDS

t19 <- t18 %>%
  group_by(
    WorkerID
  ) %>%
  summarize(
    cond = cond[[1]],
    tote = sum(
      ans %>%
        equals(1) &
        appear %>% 
        equals("appear")
    )
  ) %>%
  mutate(
    tote = tote  %>% 
      mapvalues(
        0:6,
        c(0, 1, "2+") %>%
          rep(c(1, 1, 5)
          )
      )
  ) %>% 
group_by(
  cond, tote
) %>% 
  tally %>% 
  ungroup %>% 
  complete(
    cond, tote, fill = list(n = 0)
  ) %>% 
  group_by(
    cond
  ) %>% 
  mutate(
    prop = n %>% 
      divide_by(
        n %>% 
          sum
      ) %>%
      multiply_by(100) %>% 
      round,
    tote = tote %>% 
      fct_inorder
  ) %>% 
  select(-n) %>% 
  spread(tote, prop) 

# 
# t2$stat <- c("")


t20 <- "C:/Dropbox/ethankimtom/pc methods paper/replication code and data/t13.RDS" %>% 
  readRDS

t21 <- t20 %>% 
  ddply(
    .(c2),
    function(i)
      glm(tote ~ p2, data = i, family = poisson()) %>% 
      tidy
  ) %>% 
  filter(
    term == "p2post"
  ) %>% 
  mutate(lab = 
           estimate %>% 
           round(2) %>% 
           str_c(
             "***(",
             std.error %>% 
               round(2) %>% 
               str_sub(2),
             ")")
  ) %>% 
  select(c2, lab) %>% 
  left_join(
    t20 %>% 
      ddply(
        .(c2),
        function(i)
          glm(tote ~ p2, data = i, family = poisson()) %>% 
          glance
      ) %>%  
      select(
        c2:AIC
      ) %>% 
      modify_at(
        2:5,
        ~round(., 1))
  )

